#include "form.h"
#include "ui_form.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);

    inputDevs =  QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
    foreach(QAudioDeviceInfo i,inputDevs)
        ui->inputDev->addItem(i.deviceName());


    ui->soundQua->addItem(tr("48kHz, 16bit, 2channels"));
    ui->soundQua->addItem(tr("44.1kHz, 16bit, 2channels"));
    ui->soundQua->addItem(tr("16kHz, 16bit, 2channels"));
    ui->soundQua->addItem(tr("8kHz, 16bit, 2channels"));
    ui->soundQua->addItem(tr("48kHz, 16bit, 1channels"));
    ui->soundQua->addItem(tr("44.1kHz, 16bit, 1channels"));
    ui->soundQua->addItem(tr("16kHz, 16bit, 1channels"));
    ui->soundQua->addItem(tr("8kHz, 16bit, 1channels"));

    resizeUi.init(400,450);
    resizeUi.addInfo(ui->info);
    resizeUi.addInfo(ui->label);
    resizeUi.addInfo(ui->pushButton_2);
    resizeUi.addInfo(ui->pushButton_3);
    resizeUi.addInfo(ui->pushButton_4);
    resizeUi.addInfo(ui->pushButton_5);
    resizeUi.addInfo(ui->pushButton_6);
    resizeUi.addInfo(ui->pushButton_7);
    resizeUi.addInfo(ui->inputDev);
    resizeUi.addInfo(ui->soundQua);
}

Form::~Form()
{
    delete ui;
}

void Form::on_pushButton_2_clicked()
{
    if(!isSending)
    {
        int quality=ui->soundQua->currentIndex();
        int dev=ui->inputDev->currentIndex();

        QAudioDeviceInfo inputDevInfo=inputDevs.at(dev);
        clientSend->init(dstIP,CLIENT_SEND_PORT, SERVER_RECV_PORT, quality, inputDevInfo);
        clientSend->mystart(serverSendDelay);

        char temp[100];
        sprintf(temp,"Play_%d",quality);
        tcpSocket->write(temp,20);

        isSending=true;
        ui->pushButton_2->setText(tr("Stop"));
    }
    else
    {
        char temp[100];
        sprintf(temp,"StopPlay");
        tcpSocket->write(temp,20);

        clientSend->mystop();

        isSending=false;
        ui->pushButton_2->setText(tr("Send"));
    }

}

void Form::on_pushButton_3_clicked()
{
    this->close();
}

void Form::on_pushButton_4_clicked()
{
    ui->info->appendPlainText(tr("recvBuf:")+QString::number(soundServer->serverRecv.test(1)));
}

void Form::on_pushButton_5_clicked()
{
    ui->info->appendPlainText(tr("recvBuf:")+QString::number(soundServer->serverRecv.test(-1)));
}

void Form::on_pushButton_7_clicked()
{
    serverSendDelay++;
    ui->info->appendPlainText(tr("send delay:")+QString::number(serverSendDelay));
}

void Form::on_pushButton_6_clicked()
{
    serverSendDelay--;
    if(serverSendDelay<1)
        serverSendDelay=1;
    ui->info->appendPlainText(tr("send delay:")+QString::number(serverSendDelay));
}

void Form::resizeEvent(QResizeEvent *event)
{
    resizeUi.resizes(this->width(),this->height());
}
